package cn.vcjf.washcar.userInfo.controller;


import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.userInfo.entity.User;
import cn.vcjf.washcar.userInfo.service.UserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-17
 */
@Controller
@RequestMapping("/userInfo/user")
public class UserController {
	@Resource
	private UserService userService;

	@RequestMapping("/findManagerUser")
	@ResponseBody
	public TableResult findManagerUser() {
		User user = userService.findManagerUser();
		if (user == null) {
			return TableResult.ErrorResult("没有角色为商户的用户");
		}
		return TableResult.Result(user);
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public TableResult addUser(User user) {
		user.preInsert();
		boolean b = userService.insert(user);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public TableResult updateUser(User user) {
		user.preUpdate();
		boolean b = userService.updateById(user);
		return TableResult.Result(b);
	}

	@RequestMapping("/findUserOne")
	@ResponseBody
	public TableResult findUserOne(User user) {

		EntityWrapper<User> wrapper = user.getQueryWrapper();

		User result = userService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findUserList")
	@ResponseBody
	public TableResult findUserList(User user) {

		EntityWrapper<User> wrapper = user.getQueryWrapper();

		Page<User> page = user.getPageInfo();
//		registTimeBegDate','registTimeEndDate','createTimeBegDate','createTimeEndDate
		if(null != user.getRegistTimeBegDate()){
			wrapper.ge("registTime",user.getRegistTimeBegDate());
		}
		if(null != user.getRegistTimeEndDate()){
			wrapper.le("registTime",user.getRegistTimeEndDate());
		}
		if(null != user.getCreateTimeBegDate()){
			wrapper.ge("createTime",user.getCreateTimeBegDate());
		}
		if(null != user.getCreateTimeEndDate()){
			wrapper.le("createTime",user.getCreateTimeEndDate());
		}

		page = userService.selectPageList(page, wrapper);

		return TableResult.Result(page);

	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public TableResult deleteUser(User user) {
		user.preUpdate();
		boolean b = userService.deleteById(user.getId());
		return TableResult.Result(b);
	}

	@RequestMapping("/deleteUserByIds")
	@ResponseBody
	public TableResult deleteUserByIds(String... ids) {
		List<String> list = Arrays.asList(ids);
		boolean b = userService.deleteBatchIds(list);
		return TableResult.Result(b);
	}
}
